<template>
  <div class="newsinfo-container">
    <!-- <h3>新闻详情组件{{ $route.params.id }}</h3> -->
    <!-- <h3>新闻详情组件{{ id }}</h3> -->
    <h3 class="title">{{ newsinfo.title }}</h3>
    <p class="info">
      <span>发布时间:{{ newsinfo.add_time | dateFormat }}</span>
      <span>点击:{{ newsinfo.click }}次</span>
    </p>
    <hr />
    <!-- 新闻内容 -->
    <!-- <div class="content">{{ newsinfo.content }}</div> -->
    <div class="content" v-html="newsinfo.content"></div>

    <!-- 这是评论组件的位置 -->
    <!-- 详情页作为父组件向评论子组件传递newsid，通过属性绑定的形式-->
    <comment :newsid="id"></comment>
  </div>
</template>

<script>
// 导入Comment.vue子组件
import comment from "../sub-components/Comment.vue";

export default {
  data() {
    return {
      newsinfo: {},
    };
  },
  methods: {
    async getnewsinfo() {
      // 根据id获取新闻详情
      const { data } = await this.$http.get("/api/getnew/" + this.id);
      if (data.status === 0) {
        console.log("data", data);
        return (this.newsinfo = data.message[0]);
      }
    },
  },
  created() {
    console.log(this);
    this.getnewsinfo();
  },
  props: ["id"],
  components: {
    // 为当前的NewsInfo.vue组件注册私有的子组件
    comment,
  },
};
</script>

<style lang="scss" scoped>
.newsinfo-container {
  padding: 3px;
  .title {
    font-size: 15px;
    text-align: center;
    color: red;
    margin: 15px 0;
  }
  .info {
    color: royalblue;
    display: flex;
    justify-content: space-between;
  }
}
</style>